package com.zr.lads.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.lads.service.FuncService;
import com.zr.lads.service.RoleService;
import com.zr.lads.util.PrintWriterUtil;

import net.sf.json.JSONArray;

/**
 * 处理权限管理
 * 
 * @author tang
 *
 */
@Controller
@Scope(value="prototype")
public class PermissionAction extends ActionSupport implements ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int page;  //页码
	private int rows;  //每页数据数目
	private int roleId;  //角色id
	private int[] roleFuncs;
	
	@Resource
	private RoleService roleService;
	@Resource
	private FuncService funcService;
	
	private HttpServletResponse resp;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public int[] getRoleFuncs() {
		return roleFuncs;
	}

	public void setRoleFuncs(int[] roleFuncs) {
		this.roleFuncs = roleFuncs;
	}

	/**
	 * 获取所有角色列表
	 */
	public void showRoles() {
		PrintWriterUtil.getPW(resp).write(roleService.getAllRoles(page, rows).toString());
	}
	/**
	 * 根据角色id获取对应的功能
	 */
	public void getFuncByUid(){
		String currentFunc = roleService.getRoleFuncsByRoleId(roleId);
		String allfunc = funcService.getAllFuncIds();
		JSONArray currentFuncs = funcService.getCurrentUserFuncsByParentId(-1, allfunc, currentFunc);
		PrintWriter pw = PrintWriterUtil.getPW(resp);
		pw.write(currentFuncs.toString());
	}
	/**
	 * 更新角色功能
	 */
	public void updateRoleFunc(){
		PrintWriter pw = PrintWriterUtil.getPW(resp);
		if (roleService.updateRoleFunc(roleId, roleFuncs)) {
			pw.write("1");
		} else {
			pw.write("0");
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.resp = resp;
	}

}
